package com.le.tester.imooc.oldfairy.test;

/**
 * createTime：2023/6/8 14:46
 * description：双判空检查单例
 */
public class Foo {

    static class DbConnection {
        private DbConnection() {
        }
    }

    static volatile DbConnection ref;

    public static DbConnection getDb() {
        if (ref == null) {
            synchronized (Foo.class) {
                System.out.println("enter synchronized id is " + Thread.currentThread().getId());
                if (ref == null) {
                    System.out.println("instance init..thread is " + Thread.currentThread().getId());
                    ref = new DbConnection();
                }
            }
        }
        return ref;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 2000; i++) {
            new Thread(() -> {
                getDb();
            }).start();
        }
    }
}
